// executors/executor_GitHubInfiniteMonitor.js (放在 executors 目录下)

/**
 * 演示无限次执行：持续监控 GitHub 搜索结果页面的 DOM。
 */
export async function execute(rule) {
    // 获取或设置一个页面数据属性来追踪执行次数
    const element = document.body;
    const currentCount = parseInt(element.dataset.monitorCount || 0) + 1;
    element.dataset.monitorCount = currentCount;

    // 尝试找到 GitHub 搜索结果列表
    const resultList = document.querySelector('div[data-testid="results-list"]');

    let statusMessage = "Monitor: Running...";

    if (resultList) {
        const resultCount = resultList.children.length;
        statusMessage = `Monitor: Found ${resultCount} search results.`;
        resultList.style.border = '2px solid red'; // 视觉反馈

    } else {
        statusMessage = "Monitor: Could not find results list (perhaps not a search page?).";
    }

    console.log(`[Infinite Monitor] Rule: ${rule.name} | Count: ${currentCount} | Status: ${statusMessage}`);

    // 故意在第 5 次执行时抛出错误，测试控制器的健壮性
    if (currentCount === 5) {
        console.warn("[Infinite Monitor] Intentionally throwing error on the 5th run.");
        // 抛出错误后，控制器仍会记录第 5 次，并在 3 秒后调度第 6 次
        throw new Error("Simulated API data fetch failure.");
    }
}